package com.tacx.groupassignment;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Energidesign extends Fragment implements OnTouchListener,
		OnDragListener {

	private int _xDelta;
	private int _yDelta;
	ViewGroup _root;
	private View energidesign;

	public static List<String> list = new ArrayList<String>();

	ImageView greenBrick;
	ImageView redBrick;
	ImageView yellowBrick;

	TextView greenBrickText;
	TextView redBrickText;
	TextView yellowBrickText;

	int selectedItem = 0;

	int mCurCheckPosition = 0;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		energidesign = inflater
				.inflate(R.layout.energidesign, container, false);

		energidesign.findViewById(R.id.green_brick_text).setOnTouchListener(
				this);
		greenBrickText = (TextView) energidesign
				.findViewById(R.id.green_brick_text);

		energidesign.findViewById(R.id.red_brick_text).setOnTouchListener(this);
		redBrickText = (TextView) energidesign
				.findViewById(R.id.red_brick_text);

		energidesign.findViewById(R.id.yellow_brick_text).setOnTouchListener(
				this);
		yellowBrickText = (TextView) energidesign
				.findViewById(R.id.yellow_brick_text);

		_root = (ViewGroup) energidesign.findViewById(R.id.root);
		_root.setOnDragListener(this);

		setHasOptionsMenu(true);

		if (savedInstanceState != null) {
			// Restore last state for checked position.
			mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
		}
		return energidesign;
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt("curChoice", mCurCheckPosition);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		final int X = (int) event.getRawX();
		final int Y = (int) event.getRawY();
		switch (event.getAction() & MotionEvent.ACTION_MASK) {
		case MotionEvent.ACTION_DOWN:
			RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) v
					.getLayoutParams();
			_xDelta = X - lParams.leftMargin;
			_yDelta = Y - lParams.topMargin;
			break;
		case MotionEvent.ACTION_UP:
			break;
		case MotionEvent.ACTION_POINTER_DOWN:
			break;
		case MotionEvent.ACTION_POINTER_UP:
			break;
		case MotionEvent.ACTION_MOVE:
			RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v
					.getLayoutParams();
			layoutParams.leftMargin = X - _xDelta;
			layoutParams.topMargin = Y - _yDelta;
			layoutParams.rightMargin = -250;
			layoutParams.bottomMargin = -250;
			v.setLayoutParams(layoutParams);
			break;
		}
		_root.invalidate();
		return true;
	}

	@Override
	public boolean onDrag(View v, DragEvent e) {
		if (e.getAction() == DragEvent.ACTION_DROP) {
			View view = (View) e.getLocalState();
			ViewGroup from = (ViewGroup) view.getParent();
			from.removeView(view);
			LinearLayout to = (LinearLayout) v;
			to.addView(view);
			view.setVisibility(View.VISIBLE);
		}
		return true;
	}

	public void mainMessageBox() {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle(R.string.dialog_title_main);
		builder.setItems(R.array.mainBricks_array,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						// The 'which' argument contains the index position
						// of the selected item
						selectedItem = which;
						if (selectedItem == 1) {
							yellowMessageBox();
						} else if (selectedItem == 2) {
							greenMessageBox();
						} else {
							redMessageBox();
						}
					}
				});
		builder.show();
	}

	public void greenMessageBox()
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle(R.string.dialog_title_green);
		builder.setItems(R.array.greenBricks_array,
				new DialogInterface.OnClickListener() {
			
					public void onClick(DialogInterface dialog, int which) {
						// The 'which' argument contains the index position
						// of the selected item
						ListView lw = ((AlertDialog)dialog).getListView();
						Object checkedItem = lw.getAdapter().getItem(which);
						
						if (checkedItem.toString() != "-") {
							if (checkOtherLists(checkedItem.toString())) {
								list.add(checkedItem.toString());
								
								greenBrickText.setVisibility(0);
								greenBrickText.setText(checkedItem.toString());
								
								if (list.contains(checkedItem.toString())) {
									Toast.makeText(energidesign.getContext(), String.valueOf(checkedItem + " has been added to the category"), Toast.LENGTH_SHORT).show();
								}
							} else
								Toast.makeText(
										energidesign.getContext(),
										"The item has already been added to the category!",
										Toast.LENGTH_SHORT).show();
						}
					}

				});
		builder.show();
	}
	
	public void yellowMessageBox()
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle(R.string.dialog_title_yellow);
		builder.setItems(R.array.yellowBricks_array,
				new DialogInterface.OnClickListener() {
			
					public void onClick(DialogInterface dialog, int which) {
						// The 'which' argument contains the index position
						// of the selected item
						
						ListView lw = ((AlertDialog)dialog).getListView();
						Object checkedItem = lw.getAdapter().getItem(which);
						
						if (checkedItem.toString() != "-") {
							if (checkOtherLists(checkedItem.toString())) {
								list.add(checkedItem.toString());
								yellowBrickText.setVisibility(0);
								yellowBrickText.setText(checkedItem.toString());
								
								if (list.contains(checkedItem.toString())) {
									Toast.makeText(energidesign.getContext(), String.valueOf(checkedItem + " has been added to the category"), Toast.LENGTH_SHORT).show();
								}
							} else
								Toast.makeText(
										energidesign.getContext(),
										"The item has already been added to the category!",
										Toast.LENGTH_SHORT).show();
						} else
							Toast.makeText(energidesign.getContext(), "Please select an item!", Toast.LENGTH_SHORT).show();

					}

				});
		builder.show();
	}
	
	public void redMessageBox()
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle(R.string.dialog_title_red);
		builder.setItems(R.array.redBricks_array,
				new DialogInterface.OnClickListener() {
			
					public void onClick(DialogInterface dialog, int which) {
						// The 'which' argument contains the index position
						// of the selected item
						ListView lw = ((AlertDialog)dialog).getListView();
						Object checkedItem = lw.getAdapter().getItem(which);
						
						if (checkedItem.toString() != "-") {
							if (checkOtherLists(checkedItem.toString())) {
								list.add(checkedItem.toString());
								redBrickText.setVisibility(0);
								redBrickText.setText(checkedItem.toString());
								
								if (list.contains(checkedItem.toString())) {
									Toast.makeText(energidesign.getContext(), String.valueOf(checkedItem + " has been added to the category"), Toast.LENGTH_SHORT).show();
								}
							} else
								Toast.makeText(
										energidesign.getContext(),
										"The item has already been added to the category!",
										Toast.LENGTH_SHORT).show();
						} else
							Toast.makeText(energidesign.getContext(), "Please select an item!", Toast.LENGTH_SHORT).show();

					}

				});
		builder.show();
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		menu.add("Add Brick");
		super.onCreateOptionsMenu(menu, inflater);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case 0:
			mainMessageBox();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public boolean checkOtherLists(String item) {
		boolean okay = false;
		for (int i = 0; i < 8; i++) {
			switch (i) {
			case 0:
				if (!Arkitektur.list.contains(item))
					okay = true;
				else
					okay = false;
				break;
			case 1:
				if (!BrugereFunktion.list.contains(item))
					okay = true;
				else
					okay = false;
				break;
			case 2:
				if (!Energidesign.list.contains(item))
					okay = true;
				else
					okay = false;
				break;
			case 3:
				if (!Industrialisering.list.contains(item))
					okay = true;
				else
					okay = false;
				break;
			case 4:
				if (!Okonomi.list.contains(item))
					okay = true;
				else
					okay = false;
				break;
			case 5:
				if (!Stabilitet.list.contains(item))
					okay = true;
				else
					okay = false;
				break;
			case 6:
				if (!Stedet.list.contains(item))
					okay = true;
				else
					okay = false;
				break;
			case 7:
				if (!TekniskeInstallationer.list.contains(item))
					okay = true;
				else
					okay = false;
				break;
			}
		}
		return okay;
	}
}
